extends Node2D
class_name Pieces
var pieceScene:PackedScene = preload("res://Board/scene/PieceV2.tscn")
var pieces:Array[PieceV2]

signal selected_piece_signal(PieceV2)
signal update_piece_signal(piece:PieceV2,active:bool)

func _ready() -> void:
	init_position()
	update_piece_signal.connect(_on_update_piece_hightlight)
	GlobalSignals.all_pieces_request_signal.connect(_on_all_pieces_request)

func init_position() -> void:
	# 棋子默认位置
	var pip=[
		{"x":0,"y":0,"text":"车","color":Color.RED,"type":BoardCommon.TYPE.CHARIOT},
		{"x":1,"y":0,"text":"马","color":Color.RED,"type":BoardCommon.TYPE.HORSE},
		{"x":2,"y":0,"text":"相","color":Color.RED,"type":BoardCommon.TYPE.ELEPHANT},
		{"x":3,"y":0,"text":"士","color":Color.RED,"type":BoardCommon.TYPE.ADVISOR},
		{"x":4,"y":0,"text":"将","color":Color.RED,"type":BoardCommon.TYPE.GENERAL},
		{"x":5,"y":0,"text":"士","color":Color.RED,"type":BoardCommon.TYPE.ADVISOR},
		{"x":6,"y":0,"text":"相","color":Color.RED,"type":BoardCommon.TYPE.ELEPHANT},
		{"x":7,"y":0,"text":"马","color":Color.RED,"type":BoardCommon.TYPE.HORSE},
		{"x":8,"y":0,"text":"车","color":Color.RED,"type":BoardCommon.TYPE.CHARIOT},
		{"x":1,"y":2,"text":"炮","color":Color.RED,"type":BoardCommon.TYPE.CANNON},
		{"x":7,"y":2,"text":"炮","color":Color.RED,"type":BoardCommon.TYPE.CANNON},
		
		{"x":0,"y":3,"text":"兵","color":Color.RED,"type":BoardCommon.TYPE.SOLDIER},
		{"x":2,"y":3,"text":"兵","color":Color.RED,"type":BoardCommon.TYPE.SOLDIER},
		{"x":4,"y":3,"text":"兵","color":Color.RED,"type":BoardCommon.TYPE.SOLDIER},
		{"x":6,"y":3,"text":"兵","color":Color.RED,"type":BoardCommon.TYPE.SOLDIER},
		{"x":8,"y":3,"text":"兵","color":Color.RED,"type":BoardCommon.TYPE.SOLDIER},
		
		{"x":0,"y":10,"text":"车","color":Color.BLACK,"type":BoardCommon.TYPE.CHARIOT},
		{"x":1,"y":10,"text":"马","color":Color.BLACK,"type":BoardCommon.TYPE.HORSE},
		{"x":2,"y":10,"text":"相","color":Color.BLACK,"type":BoardCommon.TYPE.ELEPHANT},
		{"x":3,"y":10,"text":"士","color":Color.BLACK,"type":BoardCommon.TYPE.ADVISOR},
		{"x":4,"y":10,"text":"将","color":Color.BLACK,"type":BoardCommon.TYPE.GENERAL},
		{"x":5,"y":10,"text":"士","color":Color.BLACK,"type":BoardCommon.TYPE.ADVISOR},
		{"x":6,"y":10,"text":"相","color":Color.BLACK,"type":BoardCommon.TYPE.ELEPHANT},
		{"x":7,"y":10,"text":"马","color":Color.BLACK,"type":BoardCommon.TYPE.HORSE},
		{"x":8,"y":10,"text":"车","color":Color.BLACK,"type":BoardCommon.TYPE.CHARIOT},
		{"x":1,"y":8,"text":"炮","color":Color.BLACK,"type":BoardCommon.TYPE.CANNON},
		{"x":7,"y":8,"text":"炮","color":Color.BLACK,"type":BoardCommon.TYPE.CANNON},
		
		{"x":0,"y":7,"text":"兵","color":Color.BLACK,"type":BoardCommon.TYPE.SOLDIER},
		{"x":2,"y":7,"text":"兵","color":Color.BLACK,"type":BoardCommon.TYPE.SOLDIER},
		{"x":4,"y":7,"text":"兵","color":Color.BLACK,"type":BoardCommon.TYPE.SOLDIER},
		{"x":6,"y":7,"text":"兵","color":Color.BLACK,"type":BoardCommon.TYPE.SOLDIER},
		{"x":8,"y":7,"text":"兵","color":Color.BLACK,"type":BoardCommon.TYPE.SOLDIER}
	]
	for p in pip:
		var piece:PieceV2 = pieceScene.instantiate()
		add_child(piece)
		#链接信号
		piece.click_piece_signal.connect(_click_piece)
		piece.position.x += p.x*BoardCommon.unit_size.x
		piece.position.y += p.y*BoardCommon.unit_size.y
		piece.side=BoardCommon.TURN.RED if p.color==Color.RED else BoardCommon.TURN.BLACK
		piece.type=p.type
		piece.set_label(p.text,p.color)
		pieces.append(piece)

func _click_piece(piece:PieceV2)->void: selected_piece_signal.emit(piece)

func _on_update_piece_hightlight(piece:PieceV2,isActive:bool) -> void:
	for item in pieces:
		if item:
			if piece==item:
				item._update_piece_hightlinght(!isActive)
			else:
				item._update_piece_hightlinght(isActive)

## 获取指定位置的棋子
func find_piece(pos:Vector2) -> PieceV2:
	var items:Array[Node] = get_children()
	for item in items:
		if item is PieceV2:
			if item.position==pos:
				return item
	return null


var cur_pieces:Array[PieceV2]=[]
## 获取所有棋子信息
func all_pieces()->Array[PieceV2]:
	var result:Array[PieceV2]=[]
	var items:Array[Node] = get_children()
	for item in items:
		if item is PieceV2:
			result.append(item)
	cur_pieces=result
	return cur_pieces

## 响应获取所有棋子信号
func _on_all_pieces_request() -> void:
	GlobalSignals.all_pieces_response_signal.emit(cur_pieces)
